package mvp.com.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by YoungerChen on 2016/9/20.
 */
public class CustomProgressBar extends View {


    private int mFirstColor;
    private int mSecondColor;
    private String mProgressText;
    private float mProgressWidth;
    private float mProgressInnerRadius;
    private int mProgressTextColor;
    private float mProgressTextSize;
    private float max;
    private int progress;
    private int mDefaultFirstColor = getResources().getColor(R.color.colorPrimary);
    private int mDefaultSecondColor = getResources().getColor(R.color.colorAccent);
    private int mDefaultProgressTextColor = getResources().getColor(R.color.colorPrimaryDark);
    private float mDefaultProgressWidth = getResources().getDimension(R.dimen.activity_horizontal_margin);

    // 宽度
    private int mWidth;
    // 高度
    private int mHeight;

    // 画笔
    private Paint mPaint;
    // 文本约束
    private Rect mTextRect;

    public CustomProgressBar(Context context) {
        this(context, null);
    }

    public CustomProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttribute(context, attrs, defStyleAttr);
        // 初始化画笔
        mPaint = new Paint();
    }

    /**
     * 初始化自定义属性
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    private void initAttribute(Context context, AttributeSet attrs, int defStyleAttr) {
        if (attrs != null) {
            TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, defStyleAttr, 0);

            //第一层颜色
            mFirstColor = typedArray.getColor(R.styleable.CustomProgressBar_firstColor, mDefaultFirstColor);
            //第二层颜色
            mSecondColor = typedArray.getColor(R.styleable.CustomProgressBar_secondColor, mDefaultSecondColor);
            //圆圈内部显示的文本
            mProgressText = typedArray.getString(R.styleable.CustomProgressBar_progressText);
            //圆圈线条宽度
            mProgressWidth = typedArray.getFloat(R.styleable.CustomProgressBar_progressWidth, mDefaultProgressWidth);
            //圆圈内径
            mProgressInnerRadius = typedArray.getFloat(R.styleable.CustomProgressBar_progressInnerRadius, 18);
            //圆圈内文本颜色
            mProgressTextColor = typedArray.getColor(R.styleable.CustomProgressBar_textColor, mDefaultProgressTextColor);
            //圆圈内文本字体大小
            mProgressTextSize = typedArray.getDimension(R.styleable.CustomProgressBar_progressTextSize, 20);
            //最大进度
            max = typedArray.getFloat(R.styleable.CustomProgressBar_max, 0);
            //当前进度
            progress = typedArray.getInt(R.styleable.CustomProgressBar_progress, 0);

        }
    }

    /**
     * 测量组件的大小，设置为wrapcontent的时候，组件的大小由半径决定
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 获取size和Mode
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        // 测量组件的宽度
        if (widthMode == MeasureSpec.EXACTLY) {
            // 如果是精准测量的话，即在使用的时候给MatchParent或者一个固定的大小
            mWidth = widthSize;
        } else {
            // 内部直径加上 左右两边的宽度
            int desire = (int) (mProgressWidth * 2 + mProgressInnerRadius);

            // wrap_content
            if (widthMode == MeasureSpec.AT_MOST) {
                mWidth = Math.min(desire, widthSize);
            }
        }

        // 测量组件的高度
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        } else {
            // 内部直径加上 左右两边的宽度
            int desire = (int) (mProgressWidth * 2 + mProgressInnerRadius);

            // wrap_content
            if (heightMode == MeasureSpec.AT_MOST) {
                mHeight = Math.min(desire, heightSize);
            }
        }

        // 保存测量的大小
        setMeasuredDimension(mWidth, mHeight);
    }

    /**
     * 重写onDraw方法
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 画第一层圆
        int length = Math.min(mWidth, mHeight);
        mPaint.setColor(mFirstColor); // 设置颜色
        mPaint.setStyle(Paint.Style.STROKE); // 设置风格空心
        mPaint.setStrokeWidth(mProgressWidth); // 设置画笔的宽度
        mPaint.setAlpha(50); // 设置透明度
        mPaint.setAntiAlias(true); // 开启锯齿效果
        mPaint.setFilterBitmap(true); // 图像在动画进行中会滤掉对Bitmap图像的优化操作，加快显示效果
        // 给定圆心的坐标，和半径，开始画圆
        canvas.drawCircle(mWidth/2, mHeight/2, length/2 - mProgressWidth/2, mPaint);

        // 第二层圆
        mPaint.setColor(mSecondColor);
        RectF oval = new RectF();
        oval.left = mWidth/2 - length/2 + mProgressWidth/2;
        oval.top = mHeight/2 - length/2 + mProgressWidth/2;
        oval.right = mWidth/2 + length/2 - mProgressWidth/2;
        oval.bottom = mHeight/2 + length/2 - mProgressWidth/2;

        Log.i("标记的进度：" , progress + "") ;
        Log.i("标记的MAX：" , max + "") ;
        int angle; // 角度
        if (max <= 0 || progress < 0) {
            angle = 0;
        } else {
            System.out.println(progress%max);
            angle = (int)(progress / max * 360);
        }

        Log.i("角度", angle + "");
        canvas.drawArc(oval, -90, angle, false, mPaint);

        //画圆圈内的文本
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setColor(mProgressTextColor);
        mPaint.setTextSize(mProgressTextSize);
        mPaint.setStyle(Paint.Style.FILL);
        if (!TextUtils.isEmpty(mProgressText)) {
            //如果有文本
            canvas.drawText(mProgressText, mWidth / 2, mHeight / 2 + mTextRect.height() / 2, mPaint);
        }
    }

    /**
     * 测量圆圈内文本的约束，即宽和高
     */
    private void initProgressTextSize() {
        //如果有文本就进行测量
        if (!TextUtils.isEmpty(mProgressText)) {
            mPaint.setTextSize(mProgressTextSize);
            mTextRect = new Rect();
            //文本的宽度约束就在textRect中
            mPaint.getTextBounds(mProgressText, 0, mProgressText.length(), mTextRect);
        }
    }
    public String getProgressText() {
        return mProgressText;
    }

    /**
     * 设置圆圈内的文本
     * @param progressText
     */
    public void setProgressText(String progressText) {
        mProgressText = progressText;
        //测量圆圈内文本的大小，即宽和高
        initProgressTextSize();
        postInvalidate();
    }

    public void setMax(int max) {
        this.max = max;
        postInvalidate();
    }

    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    }

}
